package com.aaa.ssm.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.aaa.ssm.entity.OperLog;
import com.aaa.ssm.service.OperLogService;

import com.aaa.ssm.util.Page;
import com.aaa.ssm.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @ fileName:OperLogController
 * @ description:
 * @ author:wk
 * @ createTime:2023/10/17 20:48
 * @ version:1.0.0
 */
@RestController
@RequestMapping("OperLog")
public class OperLogController extends BaseController {

    //注解注入
    @Resource
    private OperLogService operLogService;

    /*
    *
    * 带参分页查询
    * */
    @PostMapping("OperLogList")
    public Result selectAll(@RequestBody Page<OperLog> page) {
        return success(operLogService.selectAll(page));

    }


    /*
     * 更新
     * */
    @PostMapping("update")
    public Result Operupdate(@RequestBody OperLog operLog) {
        return success(operLogService.Operupdate(operLog));

    }

    /*
     * 删除
     * */
    @DeleteMapping("delete")
    public Result Operdelete(Integer operId) {
        return success(operLogService.Operdelete(operId));
    }


    /*
     * 批量删除
     * */
    @PostMapping("handlerUserDelete")
    public Result OperhandlerUserDelete(@RequestBody List<OperLog> operLogs) {
        return success(operLogService.OperdeleteBatchIds(operLogs));
    }

    /*
     * 日志查询
     * */
    @GetMapping("queryAll")
    public Result queryAll(OperLog operLog) {
        return success(operLogService.queryAll(operLog));
    }




    /**
     * Excel导出
     *
     * @param response
     * @throws IOException
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {

        List<Map<String, Object>> list = CollUtil.newArrayList();

        List<OperLog> all = operLogService.queryAll(null);
        for (OperLog operLog : all) {
            Map<String, Object> row1 = new LinkedHashMap<>();
            row1.put("日志主键", operLog.getOperId());
            row1.put("模块标题", operLog.getTitle());
            row1.put("业务类型", operLog.getBusinessType());
            row1.put("方法名称", operLog.getMethod());
            row1.put("操作类别", operLog.getOperatorType());
            row1.put("操作人员", operLog.getOperName());
            row1.put("部门名称", operLog.getDeptName());
            row1.put("请求URL", operLog.getOperUrl());
            row1.put("主机地址", operLog.getOperIp());
            row1.put("操作地点", operLog.getOperLocation());
            row1.put("请求参数", operLog.getOperParam());
            row1.put("操作状态", operLog.getStatus());
            row1.put("错误消息", operLog.getErrorMsg());
            row1.put("操作时间", operLog.getOperTime());

            list.add(row1);
        }

        // 2. 写excel
        ExcelWriter writer = ExcelUtil.getWriter(true);
        writer.write(list, true);

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = URLEncoder.encode("操纵日志", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");

        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);
        writer.close();
        IoUtil.close(System.out);
    }
    //统计
    @GetMapping("/count")
    public Result<?> count() {
        return success(operLogService.countOper());
    }

}
